unit Index;

interface

uses TASACSConfigTypes;


const odCaTon =   1; // Catch in tonnes
const odCaNum =   2; // Catch in thousands
const odCaNumWeight =   3; //
const odWeCa =    4; // Catch Weight in kg
const odWeSt =    5; // Stock Weight in kg
const odMatProp = 6; // Maturity ogive at spawning time
const odFleet =   7; // Fleet indices
const odFleetWeight =   8; // Fleet indices
const odSSB =     9; // SSB indices
const odSSBWeight =     10; // SSB indices
const odBiomass = 11; // Biomass indices
const odBiomassWeight = 12; // Biomass indices

const odFirst = odCaTon;
const odLast = odBiomassWeight;

const c_DataSourceEditCategories : Array[0..8 - 1] of Integer = (
   odCaTon, odCaNum, odWeCa, odWeSt, odMatProp, odFleet, odSSB, odBiomass);

type DataSourceInfo = record
   Id : Integer;
   Key : string;
   DefaultFile : string;
   WeightingForId : Integer;
end;

const c_DataSourcesetup : Array[odFirst..odLast] of DataSourceInfo = (
   (Id : odCaTon; Key : 'CATON'; DefaultFile : 'Caton.txt'; WeightingForId : -1),
   (Id : odCaNum; Key : 'CANUM'; DefaultFile : 'Canum.txt'; WeightingForId : -1),
   (Id : odCaNumWeight; Key : 'CANUMWEIGHT'; DefaultFile : 'Canum.txt'; WeightingForId : odCaNum),
   (Id : odWeCa; Key : 'WECA'; DefaultFile : 'WeCa.txt'; WeightingForId : -1),
   (Id : odWeSt; Key : 'WEST'; DefaultFile : 'WeCa.txt'; WeightingForId : -1),
   (Id : odMatProp; Key : 'MATPROP'; DefaultFile : 'MatProp.txt'; WeightingForId : -1),
   (Id : odFleet; Key : 'FLEET'; DefaultFile : 'Fleet.txt'; WeightingForId : -1),
   (Id : odFleetWeight; Key : 'FLEETWEIGHT'; DefaultFile : 'FleetWeight.txt'; WeightingForId : odFleet),
   (Id : odSSB; Key : 'SSB'; DefaultFile : 'SSB.txt'; WeightingForId : -1),
   (Id : odSSBWeight; Key : 'SSBWEIGHT'; DefaultFile : 'SSBWeight.txt'; WeightingForId : odSSB),
   (Id : odBiomass; Key : 'BIOMASS'; DefaultFile : 'Biomass.txt'; WeightingForId : -1),
   (Id : odBiomassWeight; Key : 'BIOMASSWEIGHT'; DefaultFile : 'BiomassWeight.txt'; WeightingForId : odBiomass)
);


const otSSQ = 0;
const otMedian = 1;
const otAMD = 2;
const otGammaLikelihood = 3;
const otGammaDeviance = 4;
const otPoisson = 5;


const rsReset = 0;
const rsRunning = 1;
const rsRunningStep = 2;
const rsPause = 3;
const rsFinished = 4;


// Process type
const ptOrdinaryAssessment = 0;
const ptSingleModelRun = 1;
const ptBootstrap = 2;
const ptRetroSpectiveRun = 3;
const ptParameterPertubation = 4;


// Index input properties
const propProcessType = 0;
const propSearchRoutine = 1;
const propModelType = 2;
const propISVPAControlType = 3;
const propPlusGroupMethod = 4;
const propFirstYear = 5;
const propLastYear = 6;
const propLastCatchYear = 7;
const propFirstAge = 8;
const propLastAge = 9;
const propFirstRefAge = 10;
const propLastRefAge = 11;
const propNumMedianElms = 12;
const propGammaShape = 13;
const propNumFleetIndices = 14;
const propNumSSBIndices = 15;
const propNumBiomassIndices = 16;
const propNumStages = 17;
const propSSBFracF = 18;
const propSSBFracM = 19;

const propRunStage = 200;
const propRunIter = 201;
const propRunDelta = 202;
const propRunObjFunc = 203;
const propRunNumParam = 300;
const propProgramVersion = 301;

const propCaTonFile = 400;
const propCaNumFile = 401;
const propCaNumWeightFile = 402;
const propWeCaFile = 403;
const propWeStFile = 404;
const propMatPropFile = 405;
const propFleetFile = 406;
const propFleetWeightFile = 407;
const propSSBFile = 408;
const propSSBWeightFile = 409;
const propBiomassFile = 410;
const propBiomassWeightFile = 411;
const propParamInFile = 412;

const propParamOutFile = 500;
const propSummaryFile = 501;
const propOutputFile = 502;
const propBootResultFile = 503;
const propBootFMeanFile = 504;
const propBootSSBFile = 505;
const propBootObjFile = 506;
const propRetroFMeanFile = 507;
const propRetroSSBFile = 508;
const propRetroRecruitFile = 509;

const propErrorStr = 600;
const propRunErrorStr = 601;

const btOrgRes = 0;
const btOrgPar = 1;
const btModRes = 2;
const btModPar = 3;
const btExtData = 4;


type
   TOptimiseLocationPtr = ^TOptimiseLocation;
   TOptimiseLocation = record
      Iter : Integer;
      Delta, Best : single;
      Improved : boolean;
   end;
   // Running states
   // Unknown, Reset, running, pause, finished


//   TSurveyIndexPtr = ^TSurveyIndex;
   TDataSource = record
//      Weight,
      FProp,
      MProp : double;
      ObjType : Integer;
      Weight : double;
      My : double;
      PlusGroupCode : Integer;
      BootType : Integer;
      Sigma : double;
      BootFile : string;
   end; // TDataSource

   TDataSourceObj = class
   public
      ObservedDataType : Integer;
      DataSource : TDataSource;
   end; // TDataSourceObj

(*   // TIndexInput -- The index input
   TIndexInputPtr = ^TIndexInput;
   TIndexInput = record
      StockTitle : string256;
      SearchRoutine : Integer;
      ModelType : Integer;
      PlusgroupMethod : Integer;
      FirstYear : integer;
      LastYear : integer;
      FirstAge : integer;
      LastAge : integer;
      FirstRefAge : integer;
      LastRefAge : integer;
      ObjLastYear : integer;
      NumMedianElms : Integer;
      GammaShape : single;
      NumFleetIndices : integer;
      NumSSBIndices : integer;
      NumBiomassIndices : integer;
      Canum : TSurveyIndex;
      FleetIndices : array[0..15 - 1] of TSurveyIndex;
      SSBIndices : array[0..5 - 1] of TSurveyIndex;
      BiomassIndices : array[0..5 - 1] of TSurveyIndex;
      InParamFileName : string256;
      OutParamFileName : string256;
      ModelFileName : string256;
      Files : array[odCaTon..odBiomass] of string256;
   end; // TIndexInput
*)
//var g_pIndexInput : TIndexInputPtr;
var g_RunIsLoaded : Boolean = false;

//var g_pOptimiseLocation : TOptimiseLocationPtr;

(*var g_IndexInput : TIndexInput;

procedure ReadIndexInput(FileName : string);
function FirstFleet : Integer;
function NumYears : Integer;
function NumAges : Integer;*)


function ObservedDataTypeDescr(I : Integer) : string;
function ObjTypeDescr(I : Integer) : string;
function BootTypeDescr(BootType : integer) : string;


implementation


function ObjTypeDescr(I : Integer) : string;
begin
   Result := '';
   case I of
      otSSQ: Result := 'SSQ';
      otMedian: Result := 'Med';
      otAMD: Result := 'AMD';
      otGammaLikelihood: Result := 'G-Lik';
      otGammaDeviance: Result := 'G-Dev';
      otPoisson: Result := 'Poisson';
   end; // case
end; // ObjTypeDescr


function ObservedDataTypeDescr(I : Integer) : string;
begin
   Result := '';
   case I of
      odCanum: Result := 'Canum';
      odFleet: Result := 'Fleet';
      odSSB: Result := 'SSB';
      odBiomass: Result := 'Biomass';
   end; // case
end; // SurveyIndexTypeDescr

(*procedure ReadIndexInput(FileName : string);
var
   F : TextFile;
begin
   AssignFile(F, FileName);
   reset(F);
   with g_IndexInput do
   begin
      readln(F, StockTitle);
      readln(F, FirstYear, LastYear);
      readln(F, FirstAge, LastAge);
      readln(F, NumFleets);
   end;
   CloseFile(F);
end; // ReadIndexInput


function FirstFleet : Integer;
begin
   Result := 1;
end; // FirstFleet


function NumYears : Integer;
begin
   Result := g_IndexInput.LastYear - g_IndexInput.FirstYear + 1;
end; // NumYears


function NumAges : Integer;
begin
   Result := g_IndexInput.LastAge - g_IndexInput.FirstAge + 1
end; // NumAges*)

function BootTypeDescr(BootType : integer) : string;
begin
   case BootType of
      btOrgRes: Result := 'Org-Res';
      btOrgPar: Result := 'Org-Par';
      btModRes: Result := 'Mod-Res';
      btModPar: Result := 'Mod-Par';
      btExtData: Result := 'File';
   end; // case
end;



end.
